<?php
	include_once(realpath(dirname(__FILE__)) . "/../../../classes/PluginsClassiques.class.php");
	include_once(realpath(dirname(__FILE__)) . "/../../../classes/Mail.class.php");
	include_once(realpath(dirname(__FILE__)) . "/../../../classes/Dossier.class.php");
	include_once(realpath(dirname(__FILE__)) . "/../../../classes/Dossierdesc.class.php");
	include_once(realpath(dirname(__FILE__)) . "/../../../classes/Variable.class.php");
	include_once(realpath(dirname(__FILE__)) . "/classes/Mailjet_liste.class.php");
	include_once(realpath(dirname(__FILE__)) . "/classes/Mailjet_mail_liste.class.php");
	include_once(realpath(dirname(__FILE__)) . "/classes/Mailjet_campagne_envoi.class.php");
	include_once(realpath(dirname(__FILE__)) . "/classes/Mailjet_desinscription.class.php");

	class Mailjet extends PluginsClassiques{

		var $api;
		
		function Mailjet(){
			$this->PluginsClassiques();
		}

		function init(){
			$query = "CREATE TABLE `mailjet_liste` (
					 `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
					 `liste` TEXT NOT NULL ,
					 `date` DATETIME NOT NULL
					  )";
			mysql_query($query, $this->link);
			
			$query = "CREATE TABLE `mailjet_mail_liste` (
					  `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
					  `liste` INT NOT NULL ,
					  `client` INT NOT NULL ,
					  `email` TEXT NOT NULL ,
					  `date` DATETIME NOT NULL
					  )";
			mysql_query($query, $this->link);

			$query = "CREATE TABLE `mailjet_campagne_envoi` (
					 `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
					 `dossier` INT NOT NULL ,
					 `campagne` TEXT NOT NULL ,
					 `liste` INT NOT NULL ,
					 `emailfrom` TEXT NOT NULL ,
					 `nomfrom` TEXT NOT NULL ,
					 `date` DATETIME NOT NULL ,
					 `statut` INT NOT NULL
					  )";
			mysql_query($query, $this->link);

			$query = "CREATE TABLE `mailjet_desinscription` (
					 `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
					 `client` INT NOT NULL ,
					 `email` TEXT NOT NULL ,
					 `date` DATETIME NOT NULL
					  )";
			mysql_query($query, $this->link);
			
			$classement = 0;
			$dossier = new Dossier();
			$query = "select max(classement) as maxclassement from $dossier->table where parent=0";
			$resul = mysql_query($query, $dossier->link);
			if(mysql_num_rows($resul)){
				$row = mysql_fetch_object($resul);
				$classement = $row->maxclassement;
			}
			
			$dossiernewsletter = new Variable();
			if(! $dossiernewsletter->charger("dossiernewsletter")){
			
				$dossier = new Dossier();
				$dossier->parent = 0;
				$dossier->ligne = 1;
				$dosier->classement = $classement;
				$lastid = $dossier->add();
				
				$dossierdesc = new Dossierdesc();
				$dossierdesc->dossier = $lastid;
				$dossierdesc->lang = 1;
				$dossierdesc->titre = "Newsletter";
				$dossierdesc->add();
				
				$dossiernewsletter->nom = "dossiernewsletter";
				$dossiernewsletter->protege = 1;
				$dossiernewsletter->cache = 1;
				$dossiernewsletter->valeur = $lastid;
				$dossiernewsletter->add();
			
			}
			
			$emailfrom = new Variable();
			$emailfrom->charger("emailfrom");

			$nomsite = new Variable();
			$nomsite->charger("nomsite");
						
			$mailjetfrom = new Variable();
			if(! $mailjetfrom->charger("mailjetfrom")){
				$mailjetfrom->nom = "mailjetfrom";
				$mailjetfrom->valeur = $emailfrom->valeur;
				$mailjetfrom->add();
			}	
			
			$mailjetnom = new Variable();
			if(! $mailjetnom->charger("mailjetnom")){
				$mailjetnom->nom = "mailjetnom";
				$mailjetnom->valeur = $nomsite->valeur;
				$mailjetnom->add();	
			}				
			
			if(! file_exists("../newsletter.php"))
				copy("../client/plugins/mailjet/template/newsletter.php", "../newsletter.php");
				
			if(! file_exists("../newsletter.html"))
				copy("../client/plugins/mailjet/template/newsletter.html", "../newsletter.html");
				
							
		}
	
		function mail($campagne, $liste = 0){
			include(realpath(dirname(__FILE__)) . "/config.php");

			$test = new Mailjet_campagne_envoi();
			if($test->charger_dossier($campagne))
				return 0;
				
			$dossierdesc = new Dossierdesc();
			$dossierdesc->charger($campagne);

			$mailjetfrom = new Variable();
			$mailjetfrom->charger("mailjetfrom");
			
			$mailjetnom = new Variable();
			$mailjetnom->charger("mailjetnom");
				
			$mail = new Mail(true);
			$mail->IsSMTP();
			$mail->Host = $serveur_smtp;
			$mail->Port = 587;
			$mail->SMTPAuth = true;
			$mail->SMTPSecure = "tls";

			$mail->Username = $cle;
			$mail->Password = $secret;

			$urlsite = new Variable();
			$urlsite->charger("urlsite");
	
			$html = file_get_contents($urlsite->valeur . "/newsletter.php?id_dossier=" . $dossierdesc->dossier);
			
			$mail->FromName = $mailjetnom->valeur;
			$mail->From = $mailjetfrom->valeur;
			$mail->Subject = $dossierdesc->chapo;
			$mail->AddCustomHeader("X-Mailjet-Campaign: " . $dossierdesc->description);
			$mail->AddCustomHeader("X-Mailjet-Partner: thelia");
			
			$mail->MsgHTML($html);
			$mail->AltBody = "Pour visualiser la newsletter : " . $urlsite->valeur . "/newsletter.php?id_dossier=" . $dossierdesc->dossier;
			
			if(! $liste){
				$client = new Client();
				$desinscription = new Mailjet_desinscription();
				
				$query = "select * from $client->table where email not in (select email from $desinscription->table)";
				$resul = mysql_query($query, $client->link);
				while($row = mysql_fetch_object($resul)){
					$client = new Client();

					$mail->AddAddress($row->email, $row->prenom . " " . $row->nom);	
					$mail->send();
					$mail->ClearAddresses();
				}
			}
			
			
			
			$newsletter = new Mailjet_campagne_envoi();
			$newsletter->dossier = $campagne;
			$newsletter->campagne = $dossierdesc->description;
			$newsletter->liste = $liste;
			$newsletter->emailfrom = $mailjetfrom->valeur;
			$newsletter->nomfrom =  $mailjetnom->valeur;
			$newsletter->date = date("Y-m-d H:i:s");
			$newsletter->statut = 1;
			$newsletter->add();
					
			
		
		}
	
	
	}


?>